﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using WindowsFormsApplication1;

namespace debugger
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        public Form1 mainForm;
        public bool usevga;
        public uint baseAddr;
        public void getbmp()
        {

            byte[] data_in = new byte[2048];
            byte[] data_bmp = new byte[2048 * 768];

            if (usevga)
            {
                mainForm.sendCmd(Form1.VGA_CONTROL_BY_DEBUGGER, 1);
            }

            if (mainForm.fast)
            {
                mainForm.getVGA(data_bmp, baseAddr);
            }
            else
            {
                //for (uint index = 0; index < 768; index++)
                //{
                //    if (usevga)
                //    {
                //        mainForm.sendCmd(Form1.VGA_DEBUGGER_READ_LINE_ADDR, index + baseAddr);//0~16k, 14bit
                //        uint ret = mainForm.sendCmdRet(Form1.VGA_DEBUGGER_READ_LINE_REQ);
                //        mainForm.receive(data_in);
                //    }
                //    else
                //    {
                //        mainForm.memBatchRead(data_in, (index + baseAddr) * 2048);
                //    }
                //
                //    for (int i = 0; i < 2048; i++)
                //    {
                //        data_bmp[i + index * 2048] = data_in[i];
                //    }
                //    this.Text = "" + index;
                //}
            }
            //bmp.Save(DateTime.Now.ToString("yyyy-MM-dd HH_mm_ss") + ".jpg");
            if (usevga)
            {
                mainForm.sendCmd(Form1.VGA_CONTROL_BY_DEBUGGER, 0);
            }

            Bitmap bmp = new Bitmap(1024, 768, PixelFormat.Format16bppRgb565);
            System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, bmp.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = 2048 * 768;
            System.Runtime.InteropServices.Marshal.Copy(data_bmp, 0, ptr, bytes);
            bmp.UnlockBits(bmpData);

            this.pictureBox1.Image = bmp;


        }
        System.Timers.Timer t = new System.Timers.Timer();
        private void Form4_Load(object sender, EventArgs e)
        {
            this.pictureBox1.MouseDown += pictureBox1_MouseDown;
            this.pictureBox1.MouseMove += pictureBox1_MouseMove;
            this.pictureBox1.MouseUp += pictureBox1_MouseUp;

            this.KeyDown += Form4_KeyDown;
            this.KeyUp += Form4_KeyUp;


            t.Interval = 100;
            t.Elapsed += t_Elapsed;
            t.Start();
            this.FormClosed += Form4_FormClosed;
        }

        void Form4_FormClosed(object sender, FormClosedEventArgs e)
        {
            t.Stop();
            t.Dispose();
        }
        class KeyEvent
        {
            public uint key;
        }
        class MouseEvent
        {
            public uint v1;
            public uint v2;
        }

        Queue<KeyEvent> keyEvent = new Queue<KeyEvent>();
        Queue<MouseEvent> mouseEvent = new Queue<MouseEvent>();

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            t.Stop();
            try
            {
                while (true)
                {
                    if (keyEvent.Count <= 0)
                    {
                        break;

                    }
                    KeyEvent ke = keyEvent.Dequeue();

                    mainForm.sendCmd(Form1.SET_DEBUG_HID, 0x82000000 | ke.key);// key;
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 1);
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 0);
                }

                while (true)
                {
                    if (mouseEvent.Count <= 0)
                    {
                        break;

                    }
                    MouseEvent me = mouseEvent.Dequeue();

                    mainForm.sendCmd(Form1.SET_DEBUG_HID, me.v1);// key;

                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 1);
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 0);

                    mainForm.sendCmd(Form1.SET_DEBUG_HID, me.v2);// key;

                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 1);
                    mainForm.sendCmd(Form1.SET_DEBUG_HID_REQ, 0);
                }


                getbmp();
                t.Start();
            }
            catch (Exception ex)
            {
                //Console.WriteLine(ex);
            }

            //Console.WriteLine("bmp");
        }

        void Form4_KeyUp(object sender, KeyEventArgs e)
        {
        }
        /*

//Prt Scr	E012E07C (E0F07CE0F012)
//Pause/Break	E11477E1F014E077 (None)

#define KEY_MINUS    0x4E //-
#define KEY_PLUS     0x55 //+ =

#define KEY_BRACKETL 0x54 // [
#define KEY_BRACKETR 0x5B // ]
#define KEY_PIPE     0x5D // | \     //
#define KEY_CAPSLOCK 0x58

#define KEY_SEMICOLN 0x4C // ;
#define KEY_QUOTES   0x52 // '

#define KEY_SHIFTL   0x12
#define KEY_QUESTION 0x4A // ? / 
#define KEY_SHIFTR   0x59
#define KEY_CTRLL    0x14
#define KEY_ALTL     0x11


#define KEY_NUMPAD_NUM_LOCK 0x77//(F077)
#define KEY_NUMPAD_MUL      0x7C//(F07C)//*
#define KEY_NUMPAD_MIN      0x7B//(F07B)//-
#define KEY_NUMPAD_PLUS     0x79//(F079)//+


#define KEY_NUMPAD_DOT      0x71//(F071)//.
#define KEY_SCROLL_LOCK     0x7E//(F07E)


#define KEY_ALTR            (0x11|0x80) //E011 (E0F011)

#define KEY_MENU            (0x2F|0x80) //E02F (E0F02F)
#define KEY_CTRLR           (0x14|0x80) //E014 (E0F014)


#define KEY_NUMPAD_ENTER    (0x5A|0x80) //E05A (E0F05A)
         */
        void Form4_KeyDown(object sender, KeyEventArgs e)
        {
            uint key = 0;
            switch (e.KeyCode)
            {
                case Keys.Back: key = 0x66; break;//#define KEY_BKSP     0x66
               case Keys.D0: key = 0x45; break;//#define KEY_0        0x45
               case Keys.D1: key = 0x16; break;//#define KEY_1        0x16
               case Keys.D2: key = 0x1E; break;//#define KEY_2        0x1E
               case Keys.D3: key = 0x26; break;//#define KEY_3        0x26
               case Keys.D4: key = 0x25; break;//#define KEY_4        0x25
               case Keys.D5: key = 0x2E; break;//#define KEY_5        0x2E
               case Keys.D6: key = 0x36; break;//#define KEY_6        0x36
               case Keys.D7: key = 0x3D; break;//#define KEY_7        0x3D
               case Keys.D8: key = 0x3E; break;//#define KEY_8        0x3E
               case Keys.D9: key = 0x46; break;//#define KEY_9        0x46
               case Keys.A: key = 0x1C; break;//#define KEY_A        0x1C 
               case Keys.B: key = 0x32; break;//#define KEY_B        0x32
               case Keys.C: key = 0x21; break;//#define KEY_C        0x21
               case Keys.D: key = 0x23; break;//#define KEY_D        0x23
               case Keys.E: key = 0x24; break;//#define KEY_E        0x24
               case Keys.F: key = 0x2B; break;//#define KEY_F        0x2B
               case Keys.G: key = 0x34; break;//#define KEY_G        0x34
               case Keys.H: key = 0x33; break;//#define KEY_H        0x33
               case Keys.I: key = 0x43; break;//#define KEY_I        0x43
               case Keys.J: key = 0x3B; break;//#define KEY_J        0x3B
               case Keys.K: key = 0x42; break;//#define KEY_K        0x42
               case Keys.L: key = 0x4B; break;//#define KEY_L        0x4B
               case Keys.M: key = 0x3A; break;//#define KEY_M        0x3A
               case Keys.N: key = 0x31; break;//#define KEY_N        0x31
               case Keys.O: key = 0x44; break;//#define KEY_O        0x44
               case Keys.P: key = 0x4D; break;//#define KEY_P        0x4D
               case Keys.Q: key = 0x15; break;//#define KEY_Q        0x15
               case Keys.R: key = 0x2D; break;//#define KEY_R        0x2D
               case Keys.S: key = 0x1B; break;//#define KEY_S        0x1B
               case Keys.T: key = 0x2C; break;//#define KEY_T        0x2C
               case Keys.U: key = 0x3C; break;//#define KEY_U        0x3C
               case Keys.V: key = 0x2A; break;//#define KEY_V        0x2A
               case Keys.W: key = 0x1D; break;//#define KEY_W        0x1D
               case Keys.X: key = 0x22; break;//#define KEY_X        0x22
               case Keys.Y: key = 0x35; break;//#define KEY_Y        0x35
               case Keys.Z: key = 0x1A; break;//#define KEY_Z        0x1A
               case Keys.Return: key = 0x5A; break;//#define KEY_ENTER    0x5A
               case Keys.Oemcomma: key = 0x41; break; //#define KEY_COMMA    0x41 // ,
               case Keys.OemPeriod: key = 0x49; break;//#define KEY_PERIOD   0x49 // .
             /*
                 *                         Oemtilde = 192,#define KEY_TILDE    0x0E //~ `
                 *                         
                        Tab = 9,//#define KEY_TAB      0x0D
                        ShiftKey = 16,
                        ControlKey = 17,
                        Menu = 18,
                        Capital = 20,
                        Escape = 27,#define KEY_ESC      0x76
                        Space = 32,#define KEY_SPACE    0x29
                        PageUp = 33,#define KEY_PAGE_UP         (0x7D|0x80) //E07D (E0F07D)
                        PageDown = 34,#define KEY_PAGE_DOWN       (0x7A|0x80) //E07A (E0F07A)
                        End = 35,#define KEY_END             (0x69|0x80) //E069 (E0F069)
                        Home = 36,#define KEY_HOME            (0x6C|0x80) //E06C (E0F06C)
                        Left = 37,#define KEY_ARROW_LEFT      (0x6B|0x80) //E06B (E0F06B)
                        Up = 38,#define KEY_ARROW_UP        (0x75|0x80) //E075 (E0F075)
                        Right = 39,#define KEY_ARROW_RIGHT     (0x74|0x80) //E074 (E0F074)
                        Down = 40,#define KEY_ARROW_DOWN      (0x72|0x80) //E072 (E0F072)
                        Insert = 45,#define KEY_INSERT          (0x70|0x80) //E070 (E0F070)
                        Delete = 46,#define KEY_DELETE          (0x71|0x80) //E071 (E0F071)
                        LWin = 91,#define KEY_WINL            (0x1F|0x80) //E01F (E0F01F)
                        RWin = 92,#define KEY_WINR            (0x27|0x80) //E027 (E0F027)
                        NumPad0 = 96, #define KEY_NUMPAD_0        0x70//(F070)
                        NumPad1 = 97, #define KEY_NUMPAD_1        0x69//(F069)
                        NumPad2 = 98, #define KEY_NUMPAD_2        0x72//(F072)
                        NumPad3 = 99, #define KEY_NUMPAD_3        0x7A//(F07A)
                        NumPad4 = 100,#define KEY_NUMPAD_4        0x6B//(F06B)
                        NumPad5 = 101,#define KEY_NUMPAD_5        0x73//(F073)
                        NumPad6 = 102,#define KEY_NUMPAD_6        0x74//(F074)
                        NumPad7 = 103,#define KEY_NUMPAD_7        0x6C//(F06C)
                        NumPad8 = 104,#define KEY_NUMPAD_8        0x75//(F075)
                        NumPad9 = 105,#define KEY_NUMPAD_9        0x7D//(F07D)
                        Multiply = 106,
                        Add = 107,
                        Separator = 108,
                        Subtract = 109,
                        Decimal = 110,
                        Divide = 111,#define KEY_NUMPAD_DIV      (0x4A|0x80) //E04A (E0F04A) // /
                        F1 = 112, #define KEY_F1       0x05
                        F2 = 113, #define KEY_F2       0x06
                        F3 = 114, #define KEY_F3       0x04
                        F4 = 115, #define KEY_F4       0x0C
                        F5 = 116, #define KEY_F5       0x03
                        F6 = 117, #define KEY_F6       0x0B
                        F7 = 118, #define KEY_F7       0x83
                        F8 = 119, #define KEY_F8       0x0A
                        F9 = 120, #define KEY_F9       0x01
                        F10 = 121,#define KEY_F10      0x09
                        F11 = 122,#define KEY_F11      0x78
                        F12 = 123,#define KEY_F12      0x07

                        // Summary:
                        //     The NUM LOCK key.
                        NumLock = 144,
                        //
                        // Summary:
                        //     The SCROLL LOCK key.
                        Scroll = 145,
                        //
                        // Summary:
                        //     The left SHIFT key.
                        LShiftKey = 160,
                        //
                        // Summary:
                        //     The right SHIFT key.
                        RShiftKey = 161,
                        //
                        // Summary:
                        //     The left CTRL key.
                        LControlKey = 162,
                        //
                        // Summary:
                        //     The right CTRL key.
                        RControlKey = 163,
                        //
                        // Summary:
                        //     The left ALT key.
                        LMenu = 164,
                        //
                        // Summary:
                        //     The right ALT key.
                        RMenu = 165,
      
                        //
                        // Summary:
                        //     The media next track key (Windows 2000 or later).
                        MediaNextTrack = 176,
                        //
                        // Summary:
                        //     The media previous track key (Windows 2000 or later).
                        MediaPreviousTrack = 177,
                        //
                        // Summary:
                        //     The media Stop key (Windows 2000 or later).
                        MediaStop = 178,
                        //
                        // Summary:
                        //     The media play pause key (Windows 2000 or later).
                        MediaPlayPause = 179,
                        //
                        // Summary:
                        //     The launch mail key (Windows 2000 or later).
                        LaunchMail = 180,
                        //
                        // Summary:
                        //     The select media key (Windows 2000 or later).
                        SelectMedia = 181,
                        //
                        // Summary:
                        //     The start application one key (Windows 2000 or later).
                        LaunchApplication1 = 182,
                        //
                        // Summary:
                        //     The start application two key (Windows 2000 or later).
                        LaunchApplication2 = 183,
                        //
                        // Summary:
                        //     The OEM 1 key.
                        Oem1 = 186,
                        //
        //
        // Summary:
        //     The OEM 1 key.
        Oem1 = 186,
        //
        // Summary:
        //     The OEM Semicolon key on a US standard keyboard (Windows 2000 or later).
        OemSemicolon = 186,
        //
        // Summary:
        //     The OEM plus key on any country/region keyboard (Windows 2000 or later).
        Oemplus = 187,
        //
        // Summary:
        //     The OEM comma key on any country/region keyboard (Windows 2000 or later).
        Oemcomma = 188,
        //
        // Summary:
        //     The OEM minus key on any country/region keyboard (Windows 2000 or later).
        OemMinus = 189,
                 * OemQuestion = 191,
                        //
                        // Summary:
                        //     The OEM 2 key.
                        Oem2 = 191,
                        //
                        // Summary:
                        //     The OEM tilde key on a US standard keyboard (Windows 2000 or later).

                        //
                        // Summary:
                        //     The OEM 3 key.
                        Oem3 = 192,
                        //
                        // Summary:
                        //     The OEM 4 key.
                        Oem4 = 219,
                        //
                        // Summary:
                        //     The OEM open bracket key on a US standard keyboard (Windows 2000 or later).
                        OemOpenBrackets = 219,
                        //
                        // Summary:
                        //     The OEM pipe key on a US standard keyboard (Windows 2000 or later).
                        OemPipe = 220,
                        //
                        // Summary:
                        //     The OEM 5 key.
                        Oem5 = 220,
                        //
                        // Summary:
                        //     The OEM 6 key.
                        Oem6 = 221,
                        //
                        // Summary:
                        //     The OEM close bracket key on a US standard keyboard (Windows 2000 or later).
                        OemCloseBrackets = 221,
                        //
                        // Summary:
                        //     The OEM 7 key.
                        Oem7 = 222,
                        //
                        // Summary:
                        //     The OEM singled/double quote key on a US standard keyboard (Windows 2000
                        //     or later).
                        OemQuotes = 222,
                        //
                        // Summary:
                        //     The OEM 8 key.
                        Oem8 = 223,
                        //
                        // Summary:
                        //     The OEM 102 key.
                        Oem102 = 226,
                        //
                        // Summary:
                        //     The OEM angle bracket or backslash key on the RT 102 key keyboard (Windows
                        //     2000 or later).
                        OemBackslash = 226,
                        //
                        // Summary:
                        //     The PROCESS KEY key.
                        ProcessKey = 229,
                        // Summary:
                        //     The PA1 key.
                        Pa1 = 253,
                        //
                        // Summary:
                        //     The CLEAR key.
                        OemClear = 254,
                        //
                        // Summary:
                        //     The bitmask to extract a key code from a key value.
                        KeyCode = 65535,
                        //
                        // Summary:
                        //     The SHIFT modifier key.
                        Shift = 65536,
                        //
                        // Summary:
                        //     The CTRL modifier key.
                        Control = 131072,
                        //
                        // Summary:
                        //     The ALT modifier key.
                        Alt = 262144,
                 */
            }

            //public const byte SET_DEBUG_HID = 0x1A;
            //public const byte SET_DEBUG_HID_REQ = 0x1B;

            //0x82){//ps2 keyboard
            KeyEvent qe = new KeyEvent();
            qe.key = key;
            keyEvent.Enqueue(qe);
        }

        void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            MouseEvent me = new MouseEvent();
            me.v1 = 0x8E000000 | (uint)e.X | btn;
            me.v2 = 0x8F000000 | (uint)e.Y;
            mouseEvent.Enqueue(me);
        }

        void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
        }

        uint btn;
        void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            btn = ((e.Button == MouseButtons.Left ? 1u : 2u) << 16);

            MouseEvent me = new MouseEvent();
            me.v1 = 0x8E000000 | ((uint)e.X);
            me.v2 = 0x8F000000 | ((uint)e.Y) | btn;
            mouseEvent.Enqueue(me);

        }
    }
}
